package com.qiang.work.test01;

import org.slf4j.LoggerFactory;

import java.io.*;
import java.net.Socket;
import java.util.logging.Logger;

public class Demo01 {
    public static void main(String[] args) throws IOException {

        //创建本地流来读取本地的图片
        //创建本地文件
        //File f1 = new File("D:\\aa");
        File f1 = new File("D:\\bb");

        sendFile(f1);
    }

    private static void sendFile(File f1) throws IOException {
        //创建客户端
        //Socket socket = new Socket("127.0.0.1", 20000);
        File[] f2 = f1.listFiles();
        //BufferedInputStream bis = null;
        for (File f3 : f2) {
            Socket socket = new Socket("127.0.0.1", 20000);
            if (f3.isFile()) {
                //是否是文件
                //是的话切割，获取文件后三位
                //通过sbstring
                String name = f3.getName();
                String sp = name.substring(name.length() - 3, name.length());
                //System.out.println(sp);
                if (sp.equals("jpg")) {
                    //创建读取流
                    String sbp = f3.getAbsolutePath();
                    BufferedInputStream bis = new BufferedInputStream(new FileInputStream(sbp));

                    //写到网络中
                    OutputStream os = socket.getOutputStream();
                    BufferedOutputStream bos = new BufferedOutputStream(os);
                    byte[] bb = new byte[1024];
                    int len;
                    while ((len = bis.read(bb)) != -1) {
                        bos.write(bb, 0, len);
                    }

                    //刷新
                    bos.flush();
                    //System.out.println("#####");

                    //给服务器一个结束标记
                    socket.shutdownOutput();

                    //从网络中获取返回信息
                    BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                    String line;
                    while ((line = br.readLine()) != null) {
                        System.out.println(line);
                    }

                    bis.close();
                }
            } else {
                sendFile(f3);
            }
            socket.close();
        }

        // socket.shutdownOutput();

    }
}
